<!-- Web Upload Data Update PHP File -->
<!-- Reveions History ...
	Rev 1.00 at 2015/06/08 by Max Yin:
		1. First time create
	Rev 1.01 at 2015/07/08 by Max Yin:
		1. 由標楷體, 改成新細明體
		2. 输入的 P/N, S/N 转大写
		3. Upload 的 FTP 增加文件视窗选择, 是否上传档案勾选判断
		4. 上传 FTP 文件
		5. 上传文件不能为中文判断
		6. Upload 的 WSID 增加重复多笔站建立
	Rev 1.01a at 2015/07/17 by Max Yin:
		1. 增加 check is_dir(), 若 ftp 上 ftp 没有建立 ftp/文件夹时, 自动建立(TA-Weibaio)
	Rev 1.02 at 2015/07/28
		1. 修正, update ftp ip 只有文件夹, 没有文件的问题。
	Rev 1.04 at 2015/09/09 by Max Yin:
		1. 新增 2D Label 的 S/N 字串范围的判斷。
	Rev 1.05 at 2015/11/22 by Max Yin:
		1. 新增加 SMT 部分的a.线别Line,b.板面Panel。
	Rev 1.07 at 2015/11/30 by Max Yin,
		1. 解决 Web 上传系统, 若有重复的 P/N, 站别, 版本名没有拦截下来, 导致重复建立站别。(PPE-Shaoxin_huang@JOYTECH)
		2. 解决 Web 下载系统, select name='webul_select_wsid', 设定错误写成 select id='webul_select_wsid'的问题。(SMT-eric_liang@joytech )
	Reb 1.08 at 2015/12/08 by Max,
		1. Web SMT upload 增加-回流焊的选项。(eric_liang)
		2. Web SMT upload/download, 由填入栏位改为下拉选单。(eric_liang)
		3. Web Upload/Downlaod 新增加 ESD 下拉选单。(jianwei_wang)
		4. 移除 ftp IP 的栏位输入, 由 Web 程式内定 FTP 路径。(Lucas)
		5. 修正重复上传, 却只有版本不对, 导致无法上传问题。(mingshen_feng)
		6. 修正若上传者, 若没有上传者名称, 会要求重新登入讯息。
-->
<html>
	<body background="pic/background_06.gif">
	<head>
		<font face="新細明體,time new roman,Arial">
		<meta http-equiv="Content-Type" content="text/html"; charset="UTF-8">
		<script type="text/javascript">
			/**
			*返回浏览器类型(ie,firefox,opera)
			*/
			function getBrowser(){
				var type = "";
				var Browser = {
				  'isIE' : (navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0),
				  'isFirefox' : navigator.userAgent.indexOf('Firefox') >= 0,
				  'isOpera' : navigator.userAgent.indexOf('Opera') >= 0
				};
				if(Browser["isIE"]==true){type='ie'};
				if(Browser["isFirefox"]==true){type='firefox'};
				if(Browser["isOpera"]==true){type='opera'};
				return type;
			}

			/**
			*生成静态进度条
			*@param progress	进度(例如：10/120,也可为文字:进度不正常时的提示文字)
			*@param id		进度条id 大家注意生成进度条时传入id，这个id就给了大家丰富的想象空间，有个这个id就可以干很多事哦~
			*@param color 进度条颜色
			*/
			function processBar(progress,id,color){
				var browser = getBrowser();
				var height = "";
				if(browser.toLowerCase()=='ie'){
					height = 28;
				}else if(browser.toLowerCase()=='firefox'){
					height = 24;
				}else if(browser.toLowerCase()=='opera'){
					height = 24;
				}
				
				var processStr = "";
				if(parseInt(progress)<90){//进度显示放在span外
					processStr = 
					// CSS formate ...
					'<div id="'+id+'" style="height:24px;text-align:left;background:white;border: 1px solid green;cursor:hand;" >'+
						'<div style="float:left;height:'+height+'px;text-align:center;background:'+color+';width:'+progress+'%;"></div>'+progress+"%"+
					'</div>';
				} else if(parseInt(progress)>=90) {//进度显示放在span中
					processStr = 
					'<div id="'+id+'" style="height:24px;text-align:left;background:white;border: 1px solid green;cursor:hand;" >'+
						'<div style="float:left;height:'+height+'px;text-align:center;background:'+color+';width:'+progress+'%;">'+progress+"%"+'</div>'+
					'</div>';
				}else{
					processStr = 
					'<div id="'+id+'" style="height:24px;text-align:center;background:white;border: 1px solid green;cursor:hand;" >'+
						'<div style="float:left;height:'+height+'px;text-align:center;background:'+color+';"></div>'+progress+
					'</div>';
				}
				
				return processStr;
			}
		</script>
	</head>
	<body>
		<center>
			<font size="5" color="#0000FF"><b>--- 程式/文件,上传中 ---</b></font><br>
			<hr>
			<?php
				require_once('class_package/common.php');
				// $tmp_pn = CRL615766000A,CRL615766007A,...
				$tmp_pn			= strtoupper($_POST['webul_pn']);
				$tmp_wsid     	= strtoupper($_POST['webul_wsid']);
				$tmp_tpver      = strtoupper($_POST['webul_mfg_ver']);
				$tmp_work_order	= strtoupper($_POST['webul_work_order']);
				$tmp_line		= strtoupper($_POST['webul_line']);
				$tmp_panel		= strtoupper($_POST['webul_panel']);
				$tmp_work_order_number     	 	= trim(strtoupper($_POST['webul_work_order_number'])); //为工单号创立
				$tmp_webul_label	     	 	= trim(strtoupper($_POST['webul_label'])); //为label号创立
				$upload_tmpname = $_FILES['webul_upload_file']['tmp_name'];
				$upload_filename= $_FILES['webul_upload_file']['name'];
				$tmp_descript   = $_POST['webul_descript'];
				$tmp_by			= $_COOKIE['login_user'];
				if ( $tmp_by == '' ) {
					echo "<script>alert('登入帐号不能为空值, 请回到 http://10.10.6.5/webul_login.html 使用者登入网页, 重新登入!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($tmp_wsid) == 0 || strlen($tmp_wsid) > 60 ) {
					echo "<script>alert('输入的-WSID(站別)或其他栏位中, 不能为空值或超过60个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($tmp_tpver) == 0 || strlen($tmp_tpver) > 30 ) {
					echo "<script>alert('输入的-TPVER(程式版本)或其他栏位中, 不能为空值或超过30个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strpos($tmp_tpver,' ') > -1 ) {
					echo "<script>alert('输入的TPVER(程式版本)的栏位中, 不能为\' \'空白,请用\'_\'替代后, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($tmp_line) == 0 || strlen($tmp_line) > 30 ) {
					echo "<script>alert('输入的-线别Line或其他栏位中, 不能为空值或超过30个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($tmp_panel) == 0 || strlen($tmp_panel) > 30 ) {
					echo "<script>alert('输入的-板面Panel或其他栏位中, 不能为空值或超过30个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($upload_filename) == 0 ) {
					echo "<script>alert('输入的-Upload File(上传文件)或其他栏位中, 不能为空值, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strpos($upload_filename,' ') > -1 || strpos($upload_filename,'+') > -1) {
					echo "<script>alert('输入的Upload File(上传文件)的栏位中, 不能为\' \'空白或'+'字符,请用\'_\'替代后, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($tmp_descript) == 0 || strlen($tmp_descript) > 255 ) {
					echo "<script>alert('输入的-Description(描述)或其他栏位中, 不能为空值或超过255个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strpos($tmp_wsid,'SMT') > -1  ) {
					if ( $tmp_line == 'N/A' ) {
						echo "<script>alert('输入的栏位中,线别Line,不能为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
						exit();
					}
					
					if ( $tmp_panel == 'N/A' ) {
						echo "<script>alert('输入的栏位中,板面Panel,不能为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
						exit();
					}
				}
				if ( strpos($tmp_wsid,'SMT') > -1 || strpos($tmp_wsid,'REPAIR') > -1 ||strpos($tmp_wsid,'ICT') > -1 || strpos($tmp_wsid,'SCT') > -1 || strpos($tmp_wsid,'H/P') > -1 || strpos($tmp_wsid,'ROUTER') > -1 || strpos($tmp_wsid,'WS-PROFILE') > -1 || strpos($tmp_wsid,'DOC') > -1 || strpos($tmp_wsid,'ESD') > -1 ) {
					if ( $tmp_work_order != 'N/A' ) {
						echo "<script>alert('输入的-Work Order(工單)应为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
						exit();
					}
				} else {
					if ( $tmp_work_order == 'N/A' ) {
						echo "<script>alert('输入的-Work Order(工單)不应为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
						exit();
					}
				}
				if ( $tmp_wsid == "FLASH" && strpos($tmp_descript,'/') == NULL ) {
					echo "<script>alert('上传的FLASH的Description(描述)必须为\'Location/ChkSum\',Ex:\'U12/BAD7\'的格式,请重新上传!!');history.go(-1);</script>";
					exit();
				}
				
				if ( $tmp_wsid == "FLASH" && empty($tmp_work_order_number)&& $tmp_work_order != "G") {
					echo "<script>alert('上传的FLASH的工单号不能为空,G工单除外，请重新上传!!');history.go(-1);</script>";
					exit();
				}
				if ( $tmp_wsid == "FLASH" && empty($tmp_webul_label)) {
					echo "<script>alert('上传的FLASH的label不能为空，请重新上传!!');history.go(-1);</script>";
					exit();
				}
				
				unset($get_pn_array);
				if ( strpos($tmp_pn,',') > -1 )
					$get_pn_array = preg_split("/[\s,]+/",$tmp_pn);
				else
					$get_pn_array[] = $tmp_pn;
				
				$PN_chkcnt		= 0;
				$getPN_ArrCnt	= count($get_pn_array);
				foreach ( $get_pn_array as $each_pn ) {
					$each_ftpip		= GetFtpPath($tmp_wsid,$tmp_tpver,$each_pn,$tmp_line,$tmp_work_order,$tmp_panel);
					if ( substr($each_ftpip,-1) != "/" ) $each_ftpip = sprintf("%s%s",$each_ftpip,"/");
					$each_ftpipfile	= sprintf("%s%s",$each_ftpip,$upload_filename);
					
					// To check user name & password limit ...
					if ( strlen($each_pn) == 0 || strlen($each_pn) > 13 ) {
						echo "<script>alert('输入的-Part Number(局部料号), 逗号\',\'后, 不能为空值或超过13个字符, 请重新输入!!');history.go(-1);</script>";
						exit();
					}
					if ( strpos($tmp_wsid,'ROUTER') > -1 || strpos($tmp_wsid,'WS-PROFILE') > -1 || strpos($tmp_wsid,'DOC') > -1 || strpos($tmp_wsid,'ESD') > -1 ) {
						if ( $each_pn != 'N/A' ) {
							echo "<script>alert('输入的栏位中,Part Number(局部料号)不为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
							exit();
						}
					} else {
						if ( strlen($each_pn) != 13 ) {
							echo "<script>alert('输入的查询栏位中-Part Number(局部料号),应该为 13 个字元,请重新输入!!');history.go(-1);</script>";
							exit();
						}
					}
					if ( strlen($each_ftpip) == 0 || strlen($each_ftpip) > 255 ) {
						echo "<script>alert('输入的-FTP Path(FTP上传路径)或其他栏位中, 不能为空值或超过255个字符, 请重新输入!!');history.go(-1);</script>";
						exit();
					}
					if ( strpos($each_ftpip,' ') > -1 ) {
						echo "<script>alert('输入的FTP Path(FTP上传路径)的栏位中, 不能为\' \'空白字符,请用\'_\'替代后, 请重新输入!!');history.go(-1);</script>";
						exit();
					}
					
					// To get each ftp ip inside of values ...
					$get_ftpip_array	= preg_split("/[\<\>\:\/\@]+/",$each_ftpip);
					$getArrCnt    = count($get_ftpip_array);
					// echo "getArrCnt = $getArrCnt<br>";
					// getArrCnt = 7
					$ftp_host    		= $get_ftpip_array[3];
					$ftp_username		= $get_ftpip_array[1];
					$ftp_password		= $get_ftpip_array[2];
					$start = strpos($each_ftpip,$get_ftpip_array[3])+strlen($get_ftpip_array[3]);	//检索字符串中首次出现的位置。
					// $getEndStr    = $get_ftpip_array[$getArrCnt-2];
					$EndStrLen    = strlen($get_ftpip_array[$getArrCnt-2]);
					$getEndStr    = strripos($each_ftpip, $get_ftpip_array[$getArrCnt-2]);
					$getEndStr    += $EndStrLen;									//检索字符串中末尾出现的位置。
					$end          = $getEndStr;
					$ftp_destTmp        = substr($each_ftpip,$start,$end-$start);
					$ftp_destDir        = sprintf("%s/",$ftp_destTmp);
					
					// 判断上传文件不能为中文, 而关 gbk是双字节，utf8是三字节，可以根据中文的范围来判断 
					$get_file_name_array  = preg_split("/[\_\-\(\)\.\<\>]+/",$upload_filename);
					$err_chk = 0;
					foreach ( $get_file_name_array as $each_name ) {
						if( preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$each_name) ) {
							$err_chk = 1;
							break;
						}
					}
					if ( $err_chk == 1 ) {
						echo "<script>alert('输入的FTP IP(FTP文件上传路径)栏位中,不能有中文(汉)字.');history.back(-1);</script>";die;
						exit();
					}
					
					// open connection & send access parameters ...
					$conn         = ftp_connect($ftp_host);
					$login_result = ftp_login($conn, $ftp_username,$ftp_password);
					if ( (!$conn) || (!$login_result) ) { 
						echo "<script>alert('FTP 登入失败, 请确认登入IP,帐号及密码是否正确后, 再试一次!!');history.go(-1);</script>";
						exit();
					}
					ftp_pasv($conn,true); // 打开被动模式传输 (PASV MODE)，否则则关闭被动传输模式。 
					
					// delete ftp old file ...
					if ( is_file($each_ftpipfile) ) {
						unlink($each_ftpipfile);
					} elseif ( !is_dir($each_ftpip) ) {
						// if not found ftp path then reate it.
						if ( !mkdir($each_ftpip, 0777, true) ) {
							ftp_close($conn);
							echo "<script>alert('建立 FTP 上传的文件夹失败, 请再重新操作一次!!');history.go(-1);</script>";
							exit();
						}
					}
				
					// perform. file upload then to check upload status, display message ...
					echo "<br>";
					// filesize($file)
					// 开始
					$ret = ftp_nb_put($conn,$ftp_destDir.$upload_filename,$upload_tmpname,FTP_BINARY);
					$iCnt = 1;
					while ( $ret == FTP_MOREDATA ) {
						// 加入其它要执行的代码
						if ( $iCnt%100 == 0 )
							echo ".<br>";
						else 
							echo ".";

						// 继续传送...
						$ret = ftp_nb_continue($conn);
						$iCnt++;
					}

					if ( $ret != FTP_FINISHED ) {
						ftp_close($conn);
						echo "<script>alert('FTP 上传文件中发生失败, 请再试一次!!');history.go(-1);</script>";
						exit();
					}
				
					// close the FTP stream
					ftp_close($conn);
				
					// delete local copy of uploaded file
					$PN_chkcnt++;
					if ( $PN_chkcnt == $getPN_ArrCnt ) {
						if ( !unlink($upload_tmpname) ) {
							echo "<script>alert('暂存文件删除失败, 请再重新操作一次!!');history.go(-1);</script>";
							exit();
						}
					}
					
					//使用逗号或空格(包含" ", \r, \t, \n, \f)分隔短语
					unset($get_wsid_array);
					if ( strpos($tmp_wsid,',') > -1 )					
						$get_wsid_array = preg_split("/[\s,]+/",$tmp_wsid);
					else
						$get_wsid_array[] = $tmp_wsid;
					
					$Station_Obj = new Stations();
					$err_chk     = '';
					foreach ( $get_wsid_array as $each_wsid ) {
						// To check the WSID is exist ...
						$get_StationChk = $Station_Obj->Station_Exist($each_wsid);
						if ( $get_StationChk == 0 ) {
							$err_chk = 'Station_not_exist';
							break;
						}
						
						// To update the account by user name from MySQL ...
						date_default_timezone_set("Asia/Taipei");	// 定义时区为 亚洲/台北
						$each_date = date('Y-m-d G:i:s');			// 定义显示时间格式 2015-06-14 14:24:30
						if ( $tmp_wsid == "FLASH") {
							$result = download_update_order($each_pn,$each_wsid,$tmp_tpver,$tmp_work_order,$tmp_line,$tmp_panel,$each_ftpipfile,$each_date,$tmp_by,$tmp_descript,$tmp_work_order_number,$tmp_webul_label);
						}else {
							$result = download_update($each_pn,$each_wsid,$tmp_tpver,$tmp_work_order,$tmp_line,$tmp_panel,$each_ftpipfile,$each_date,$tmp_by,$tmp_descript);	
						}												
						if ( !$result ) {
							
							$err_chk = 'Update_Fail';
							break;
						}
					}
					
					if ( $err_chk == 'Station_not_exist' ) {
						echo "<script>alert('在 WSID(站別) 栏位中, 有不存在的站别, 请重新再输入一次!!');history.go(-1);</script>";
						exit();
					} elseif ( $err_chk == 'Update_Fail' ) {
						echo "<script>alert('该程式, 修改失败! 可能该笔资料不存在, 或联系程式开发人员, 并查明原因...');history.go(-1);</script>";
						exit();
					}
				}
				echo "<script>alert('该程式, 已修改成功!');history.go(-3);</script>";
				exit();
			?>
		</center>
	</body>
</html>

